<?php
require_once 'BaseController.php';

/**
 * 【PC版】保存した条件から探す コントローラ
 */
class Renew_FavoriteListController extends Renew_BaseController
{

    /**
     * メインアクション
     */
    public function indexAction()
    {
        header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
        header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // 過去の日付
        $params = $this->getParameters();
        $favoriteList = $_COOKIE['condition1'];
        // Noデータ
        if (!isset($favoriteList) || empty($favoriteList) || $favoriteList == '')
        {
            $this->_data['NoData'] = true;
            $this->displayBody2('index');
            return;
        }

        // 検索条件１
        $this->_data['cons1'] = true;

        // 条件１
        $vals = explode('|', $favoriteList);
        $consParams = array();
        for ($i = 0; $i < sizeof($vals); $i++)
        {
            $prs = explode(':', $vals[$i]);
            if ($prs[1] != '0' && $prs[1] != '' && $prs[1] != '9999999' && $prs[1] != '99999')
            {
                if (($prs[0] == 'senyu1' || $prs[0] == 'senyu2') && $prs[1] == '1') continue;
                $consParams[$prs[0]] = $prs[1];
            }
            if ($prs[0] == "search_type")
            {
                $this->_data['type1'] = $prs[1];
            }
        }
        if (sizeof($consParams) == 0)
        {
            $this->_data['NoData'] = true;
            $this->displayBody2('index');
            return;
        }
        // 条件カウント
        $this->makeConditionParams($consParams, '1');

        // ★★★条件２
        $favoriteList2 = $_COOKIE['condition2'];
        // Noデータ
        if (!isset($favoriteList2) || empty($favoriteList2) || $favoriteList2 == '')
        {
            $this->displayBody2('index');
            return;
        }
        $this->_data['cons2'] = true;
        // 条件2
        $vals2 = explode('|', $favoriteList2);
        $consParams2 = array();
        for ($i = 0; $i < sizeof($vals2); $i++)
        {
            $prs = explode(':', $vals2[$i]);
            if ($prs[1] != '0' && $prs[1] != '' && $prs[1] != '9999999' && $prs[1] != '99999')
            {
                if (($prs[0] == 'senyu1' || $prs[0] == 'senyu2') && $prs[1] == '1') continue;
                $consParams2[$prs[0]] = $prs[1];
            }
            if ($prs[0] == "search_type")
            {
                $this->_data['type2'] = $prs[1];
            }
        }
        if (sizeof($consParams2) == 0)
        {
            $this->_data['cons2'] = false;
            $this->displayBody2('index');
            return;
        }
        // 条件カウント
        $this->makeConditionParams($consParams2, '2');

        // ★★★条件２

        // ★★★条件3
        $favoriteList3 = $_COOKIE['condition3'];
        // Noデータ
        if (!isset($favoriteList3) || empty($favoriteList3) || $favoriteList3 == '')
        {
            $this->displayBody2('index');
            return;
        }
        $this->_data['cons3'] = true;
        // 条件2
        $vals3 = explode('|', $favoriteList3);
        $consParams3 = array();
        for ($i = 0; $i < sizeof($vals3); $i++)
        {
            $prs = explode(':', $vals3[$i]);
            if ($prs[1] != '0' && $prs[1] != '' && $prs[1] != '9999999' && $prs[1] != '99999')
            {
                if (($prs[0] == 'senyu1' || $prs[0] == 'senyu2') && $prs[1] == '1') continue;
                $consParams3[$prs[0]] = $prs[1];
            }
            if ($prs[0] == "search_type")
            {
                $this->_data['type3'] = $prs[1];
            }
        }
        if (sizeof($consParams3) == 0)
        {
            $this->_data['cons3'] = false;
            $this->displayBody2('index');
            return;
        }
        // 条件カウント
        $this->makeConditionParams($consParams3, '3');

        // ★★★条件２

        $this->displayBody2('index');
    }

    // 検索バラメータ
    public function makeMainParams($params, $cat, $code, $chous = null)
    {
        // 地域検索
        if ($cat == 'area')
        {
            $params = $this->addCodesToParams2($params, $code, 'PrefCd', 'CityCd');
            // 町名
            if (isset($chous) && $chous != null && sizeof($chous) > 0)
            {
                $params['TownCd'] = $chous;
            }
        }
        // 路線検索
        else if ($cat == 'line')
        {
            $params = $this->addCodesToParams2($params, $code, 'PrefCd', 'StCd');
            $params = $this->makeRailCodeParams($params, 'StCd');
        }
        return $params;
    }

    // 条件としてのデータを準備する
    public function makeConditionParams($consParams, $index)
    {
        $cat = $consParams['search_type'];

        $params = $consParams;

        // ターゲットパラメータ準備
        $codes = $this->extractParams($params, 'target_');
        $chous = $this->extractParams($params, 'targetchou_');

        $params = $this->makeMainParams($params, $cat, $codes, $chous);

        $model = new Renew_Model_BukkenList();
        $model_common = new Rstobi_Model_Common();
        $tmp = array();
        $where = '';
        $where .= ' ' . $model_common->getKihonWhere($params, $tmp, true, true);
        $where .= ' ' . $model_common->getKodawariWhere($params, $tmp);

        if ($cat == 'area')
            $params['SrhKbn'] = Apm_Config_Search::SRHKBN_JYUSYO;
        else if ($cat == 'line')
            $params['SrhKbn'] = Apm_Config_Search::SRHKBN_ENSENEKI;
        else if ($cat == 'shop')
        {
            $params['SrhKbn'] = Apm_Config_Search::SRHKBN_TENPOBUKKEN;
            $params['shopCd'] = $params['target_0'];
        }

        $dbParams = array();
        foreach ($tmp as $key => $val)
        {
            $dbParams[':' . $key] = $val;
        }

        $prefCds = "";
        $c = 0;
        foreach ($params['PrefCd'] as $cd)
        {
            $key = ":PrefCd_" . $c;
            if ($c > 0) $prefCds .= " , ";
            $prefCds .= $key;
            $dbParams[$key] = $cd;
            $c++;
        }
        $buken = null;
        $count = 0;
        $dbParams['favirote'] = true;
        $from = '';
        $OemFlg = ((array_key_exists('OemCd', $params) == true) && (is_null($params['OemCd']) == false) && (is_numeric($params['OemCd']) == true));
        // 市区町村検索
        if ($params['SrhKbn'] == Apm_Config_Search::SRHKBN_JYUSYO || $params['SrhKbn'] == Apm_Config_Search::SRHKBN_KODAWARI)
        {

            // 町検索があれば→検索する
            $cities = array();
            $chous = array();
            if (isset($params['TownCd']))
            {
                foreach ($params['TownCd'] as $val)
                {
                    $city = substr($val, 0, 5);
                    if (!in_array($city, $cities)) $cities[] = $city;
                    $chous[] = substr($val, 2);
                }
            }
            // 町検索があれば→検索する

            $cityCds = "";
            $c = 0;
            foreach ($params['CityCd'] as $cd)
            {
                if (!in_array($cd, $cities))
                { // 町からのぞく
                    $key = ":CityCd_" . $c;
                    if ($c > 0) $cityCds .= " , ";
                    $cityCds .= $key;
                    $dbParams[$key] = substr($cd, 2);
                    $c++;
                }
            }
            $data = $model->getCountForArea($prefCds, $cityCds, $chous, $from, $where, $dbParams);
            $count = $data['TATEMONO_COUNT'];
            // Pickup
            if ($count > 0)
            {
                $d = $model->getPickupForArea($prefCds, $cityCds, $chous, $from, $where, $dbParams);
                $d['T03_IMAGE_DATA'] = Apm_Bukkenitems::GetImageDataMain($d, $OemFlg);
                $d['T03_NEW_ICON'] = Apm_Bukkenitems::GetNewIcon($d);
                $d['T03_YACHIN_STR'] = Apm_Bukkenitems::GetChinryouStr($d); // 賃料
                $d['T03_MADORI_STR'] = Apm_Bukkenitems::GetMadoriStr($d); // 間取り
                $buken = $d;
            }
        }
        // 路線検索
        else if ($params['SrhKbn'] == Apm_Config_Search::SRHKBN_ENSENEKI)
        {
            $stCds = "";
            $c = 0;
            foreach ($params['StCd'] as $cd)
            {
                $key = ":StCd_" . $c;
                if ($c > 0) $stCds .= " , ";
                $stCds .= $key;
                $dbParams[$key] = $cd;
                $c++;
            }
            $data = $model->getCountForLine($prefCds, $stCds, $from, $where, $dbParams);
            $count = $data['TATEMONO_COUNT'];
            // Pickup
            if ($count > 0)
            {
                $d = $model->getPickupForLine($prefCds, $stCds, $from, $where, $dbParams);
                $d['T03_NEW_ICON'] = Apm_Bukkenitems::GetNewIcon($d);
                $d['T03_YACHIN_STR'] = Apm_Bukkenitems::GetChinryouStr($d); // 賃料
                $d['T03_MADORI_STR'] = Apm_Bukkenitems::GetMadoriStr($d); // 間取り
                $d['T03_IMAGE_DATA'] = Apm_Bukkenitems::GetImageDataMain($d, $OemFlg);
                $buken = $d;
            }
        }
        // ショップ検索
        else if ($params['SrhKbn'] == Apm_Config_Search::SRHKBN_TENPOBUKKEN)
        {
            $data = $model->getCountForShop($params['shopCd'], $from, $where, $dbParams);
            $count = $data['TATEMONO_COUNT'];
            // Pickup
            if ($count > 0)
            {
                $d = $model->getPickupShop($params['shopCd'], $from, $where, $dbParams);
                $d['T03_IMAGE_DATA'] = Apm_Bukkenitems::GetImageDataMain($d, $OemFlg);
                $d['T03_NEW_ICON'] = Apm_Bukkenitems::GetNewIcon($d);
                $d['T03_YACHIN_STR'] = Apm_Bukkenitems::GetChinryouStr($d); // 賃料
                $d['T03_MADORI_STR'] = Apm_Bukkenitems::GetMadoriStr($d); // 間取り
                $buken = $d;
            }
        }

        $this->_data['count' . $index] = $count;
        $this->_data['buken' . $index] = $buken;
    }

    /**
     * SEO対策用のmetaタグに配置する文章を設定
     */
    protected function setSeoMeta()
    {
        $this->_meta_title = '保存した条件から賃貸物件を探す 【アパマンショップ】';
        $this->_meta_keywords = '各種お問い合わせ｜';
    }

}
?>